<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :init-param="initParam">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button type="primary" :icon="Download" plain @click="downloadFile">导出数据</el-button>
      </template>
    </ProTable>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ref, reactive } from "vue";
import { ExportObj, UserAction } from "@/api/types/base";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { Download } from "@element-plus/icons-vue";
import apiService from "@/api/services/apiService";
import { useDateFormat } from "@vueuse/core";
import { ElMessageBox } from "element-plus";
import { useDownload } from "@/hooks/useDownload";
import { ExportColType } from "@/enums/sysEnum";
import { timeRender } from "@/hooks/useColumnRender";
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
  condition: {},
  options: {
    sort: { optDate: -1 }
  }
});

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  let newParams = JSON.parse(JSON.stringify(params));
  if (newParams.condition && newParams.condition.optDate) {
    let curDate = new Date(newParams.condition.optDate);
    let [year, month, day] = [curDate.getFullYear(), curDate.getMonth(), curDate.getDate()];
    let startDate = new Date(year, month, day);
    let endDate = new Date(year, month, day, 23, 59, 59);
    newParams.condition.optDate = { $gte: startDate, $lte: endDate };
  }
  return apiService.findPage("userAction", newParams.condition, newParams.options);
};

// 表格配置项
const columns = reactive<ColumnProps<UserAction>[]>([
  { type: "index", label: " ", width: 85 },
  { prop: "token", label: "令牌", width: 320 },
  // { prop: "businessId", label: "业务ID", width: 200 },
  {
    prop: "businessType",
    label: "业务类型",
    width: 160,
    // 字典请求携带参数
    enum: () => apiService.findClassify("BUSINESS_TYPE"),
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "name", value: "value" }
  },
  {
    prop: "optType",
    label: "业务操作",
    width: 100,
    // 字典请求携带参数
    enum: () => apiService.findClassify("BUSINESS_OPT_TYPE"),
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "name", value: "value" }
  },
  { prop: "optInfo", label: "操作信息", align: "left", search: { el: "input", like: true } },
  {
    prop: "optUser",
    label: "操作用户",
    width: 100,
    search: { el: "select", props: { filterable: true } },
    // 字典请求携带参数
    enum: () => apiService.find("users", {}),
    fieldNames: { label: "userName", value: "_id" }
  },
  {
    prop: "optDate",
    label: "操作时间",
    width: 200,
    render: timeRender,
    search: { el: "date-picker", props: { type: "date" }, order: 1, label: "操作时间" }
  }
]);

// 导出用户列表
const downloadFile = async () => {
  const params: ExportObj = {
    params: proTable.value?.searchParam,
    tableName: "userAction",
    enumTypes: ["BUSINESS_TYPE", "BUSINESS_OPT_TYPE"],
    tableConfigs: [{ tableName: "users", fields: ["_id", "userName"] }],
    header: [
      { header: "序号", width: 10, ext: { prop: "index", type: ExportColType.INDEX } },
      { header: "业务ID", width: 10, ext: { prop: "businessId", type: ExportColType.DEFAULT } },
      { header: "业务类型", width: 10, ext: { prop: "businessType", type: ExportColType.ENUM, key: "BUSINESS_TYPE" } }
    ]
  };
  ElMessageBox.confirm("确认导出数据?", "温馨提示", { type: "warning" }).then(() =>
    useDownload(apiService.commonExport, `操作日志-${useDateFormat(new Date(), "YYYY-MM-DD").value}`, "userAction", params)
  );
};
</script>
